/*----------------------------------------------
| html box  v1.0 www.97521.com                 |
| rebin 2016-10-27                             |
|---------------------------------------------*/
var nokahtmlbox = Class.create({
    version: '1.0',
    initialize : function(cfg) {
    	this.cfg = cfg;					//配置信息
    	this.id=cfg.id; 				//id
    	this.onclose = cfg.onclose;		//在半闭时调用的js方法
    	this.width = cfg.width;
    	this.height = cfg.height;
    	this.title = cfg.title;
    	this.gridids = cfg.gridids;
    	this.stopclose = (undefined == cfg.stopclose?false:cfg.stopclose);
    },
    show : function(title){
    	var self = this;
    	self.events();
    	self.iframebodyinit();
    },
    //--------------初始化iframe--------------------------------
    iframebodyinit : function(url){
    	var self = this;
    	var body=$(self.id+'_boxid_body');
    	var str = body.innerHTML;
    	if(undefined!=url){
    		str=url;
    	}
    	try{
	    	if(str.toLowerCase().startWith('http://') || str.toLowerCase().startWith('https://')){
	    		body.innerHTML='<iframe src="'+str+'"  id="'+self.id+'_iframe" style="width:10px;height:10px"  frameborder="0" ></iframe>';
	    	}
    	}catch(e){}
    },
    //-------------重设iframe大小-----------------------------
    iframebody : function(width,height){
    	var self = this;
    	var body=$(self.id+'_iframe');
    	try{
    		if(undefined!=body){
    			height = height-$(self.id+'_boxid_tite').getHeight()-15;
    			body.setStyle({width:width+'px',height:height+'px'});
    		}
    	}catch(e){}
    	$(self.id).fire("widget:noka_resetSelectHeight", {});//触发控件初始事件
    	setTimeout(function(){
    		try{$(self.id+'_iframe').contentWindow.document.fire("widget:noka_grid_resize", {});}catch(e){}
    	}, 200);
    	try{
    		setTimeout(function(){
    			document.fire("widget:noka_grid_resize", {});//触发控件初始事件
    		},200);
    	}catch(e){}
    },
    events : function(){
    	var self = this;
    	var obj = $(self.id);
    	obj.showWindow = function(cfg){//{title:'',width:'',height:'',url:''}
    		obj.show();
    		//-----------自定义参数-----------------------------------------------
    		if(undefined!=cfg){
    			if(undefined!=cfg.title){
    				self.title = cfg.title;
    			}
    			if(undefined!=cfg.width){
    				self.width = cfg.width;
    			}
    			if(undefined!=cfg.height){
    				self.height = cfg.height;
    			}
    			if(undefined!=cfg.onclose){
    				self.onclose = cfg.onclose;
    			}
    			if(undefined!=cfg.url){
        			self.iframebodyinit(cfg.url);
        		}
    		}
    		//----------------------------------------------------------------------
    		if(undefined!=self.title){
    			$(self.id+'_boxid_tite').innerHTML=self.title;
    			$(self.id+'_boxid_tite').show();
    		}
    		$(self.id+'_boxid_tmask').setStyle({width:self.total(0)+'px',height:self.total(1)+'px'});
    		self.position();
    	};
    	obj.close = function(){
    		obj.hide();
    		if(undefined!=self.onclose){
    			try{self.onclose($(self.id));}catch(e){}
    		}
    	};
    	$(self.id+'_boxid_tclose').observe('click',function(){
    		obj.hide();
    		if(undefined!=self.onclose){
    			try{self.onclose($(self.id));}catch(e){}
    		}
    	});
    	Event.observe(window, 'resize', function(){
    		var bheight = $(self.id+'_boxid_tbox').getHeight();
    		var theight = self.bodyHeight();
    		theight = (bheight>theight?theight+20:theight);
    		$(self.id+'_boxid_tmask').setStyle({width:self.total(0)+'px',height:self.total(1)+'px'});
    		self.position();
    	});
    	$(self.id+'_boxid_tmask').observe('click', function(event){
    		if(!self.stopclose){
	    		try{$(self.id).hide();}catch(e){}
	    		if(undefined!=self.onclose){
	    			try{self.onclose($(self.id));}catch(e){}
	    		}
    		}
    	});
    },
    //-------------------------------------------------------------------
    position : function(){
    	var self = this;
    	var tbox=$(self.id+'_boxid_tbox');
    	var width = self.width;
    	var height = self.height;
    	var top = (self.fheight()-height)/2+self.top();
    	var left = (self.fwidth()-width)/2;
    	top = (top<20?20:top);
    	tbox.setStyle({top:top+'px',left:left+'px'});
    	$(self.id+'_boxid_tinner').setStyle({width:width+'px',height:height+'px'});
    	self.iframebody(width,height);
    	try{
    		if(undefined!=self.gridids){
				for(var i=0;i<self.gridids.length;i++){
					try{
    					if('true'==self.gridids[i].autosize){
    						$(self.gridids[i].grid).setAutosize(self.gridids[i].autosize,self.gridids[i].autowidth);
    						$(self.gridids[i].grid).setWidthHeihgt((width-10)+'px',height+'px');
    						$(self.gridids[i].grid).setSize(0,height);
    						$(self.gridids[i].grid).reinit();
    					}
					}catch(e){}
				}
			}
    	}catch(e){}
    },
    //----------------------------------------------------------------------------------
    bodyWidth : function(){
    	return document.body.scrollWidth;
    },
    bodyHeight : function(){
    	return document.body.scrollHeight;
    },
    top:function(){
    	return document.documentElement.scrollTop||document.body.scrollTop;//$(self.id+'_div_title').scrollLeft
    },
	fwidth:function(){
		return self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth;
	},
	fheight:function(){
		return self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight;
	},
	total:function(d){
		var b=document.body, e=document.documentElement;
		return d?Math.max(Math.max(b.scrollHeight,e.scrollHeight),Math.max(b.clientHeight,e.clientHeight)):
		Math.max(Math.max(b.scrollWidth,e.scrollWidth),Math.max(b.clientWidth,e.clientWidth));
	}
});